package com.orphan.audit.service.impl;

import com.orphan.audit.po.Deposit;
import com.orphan.audit.dao.DepositMapper;
import com.orphan.audit.po.Save;
import com.orphan.audit.service.IDepositService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orphan.util.dto.DepositDto;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wdn
 * @since 2021-07-15
 */
@Service
public class DepositServiceImpl extends ServiceImpl<DepositMapper, Deposit> implements IDepositService {

    @Resource
    DepositMapper depositMapper;

    @Override
    public List<Deposit> findAll() {
        return depositMapper.findAll();
    }

    @Override
    public Integer saveMoney(Save save) {
        Long userId = save.getTUserid();
        float sum = save.getTSum();
        Deposit deposit = this.queryDeposit(userId);
        //通过用户ID查询是否有该用户数据,如果不为null,说明是老用户,将存钱数量sum加到该用户的total(累计预存款)和balance(预存款余额)中
        if(deposit != null) {
            deposit.setTTotal(deposit.getTTotal() + sum);
            deposit.setTBalance(deposit.getTBalance() + sum);
            return this.update(deposit);
        }
        //如果用户数据为null,说明是新用户,total和balance值等于此次存钱金额sum
        Deposit depositNew = new Deposit();
        depositNew.setTUserid(userId);
        depositNew.setTTotal(sum);
        depositNew.setTBalance(sum);
        return depositMapper.insert(depositNew);
    }

    @Override
    public Integer exist(Long id) {
        return depositMapper.exist(id);
    }

    @Override
    public Deposit queryDeposit(Long id) {
        return depositMapper.queryDeposit(id);
    }

    @Override
    public Integer update(Deposit deposit) {
        return depositMapper.update(deposit) ;
    }

    @Override
    public Integer reduce(Deposit deposit) {
        return depositMapper.reduce(deposit);
    }


}
